package GUI;

import Run_System.Exercises;
import Run_System.FileOperator;

import javax.swing.*;
import javax.swing.text.StyledEditorKit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class DisplayRecordPanel extends JPanel {
    private Exercises exercises;
    private ArrayList<Integer> answers;
    private ArrayList<Boolean> results;
    private int index = 0;
    public DisplayRecordPanel(){
        setLayout(new BorderLayout());
        //标题栏
        JLabel label = new JLabel("昨夜西风凋碧树，独上高楼，望尽天涯路"); //标题框，提醒小明要坚持不懈
        Font font = new Font("微软雅黑",Font.BOLD,16);
        label.setFont(font);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        add(label,BorderLayout.NORTH);
        //文件显示面板栏
        String[] exerciseFiles = FileOperator.readExercisesFile().toArray(new String[0]); //读取所有存储的习题文件，以列表形式供小明选择
        JPanel recordPanel = recordPanel(exerciseFiles);
        add(recordPanel,BorderLayout.CENTER);
        //下方按钮栏
        JPanel buttonPanel = new JPanel();
        if(index < exerciseFiles.length) {
            JButton nextBtn = new JButton("下一页");
            nextBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (index >= exerciseFiles.length) {     //如果文件读取完毕，则添加返回主页按钮
                        buttonPanel.remove(nextBtn);
                        JButton returnBtn = new JButton("返回主页");
                        returnBtn.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                removeAll();
                                add(new FrontPage());
                                validate();
                            }
                        });
                        buttonPanel.add(returnBtn);
                    } else {             //如果文件没读完，则显示下一页文件
                        JPanel recordPanle_next = recordPanel(exerciseFiles);
                        add(recordPanle_next, BorderLayout.CENTER);
                        validate();
                    }
                }
            });
            buttonPanel.add(nextBtn);
        }else {
            JButton returnBtn = new JButton("返回主页");
            returnBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    removeAll();
                    add(new FrontPage());
                    validate();
                }
            });
            buttonPanel.add(returnBtn);
        }
        add(buttonPanel,BorderLayout.SOUTH);
    }


    private JPanel recordPanel(String[] exerciseFiles){  //每次显示五个习题的做题结果
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(600,300));
        panel.setLayout(new GridLayout(3,4));
        JLabel headLabel1 = new JLabel("做题日期");   //添加表头
        JLabel headLabel2 = new JLabel("做题数量");
        JLabel headLabel3 = new JLabel("正确率");
        JLabel headLabel4 = new JLabel("神秘按钮");
        panel.add(headLabel1);
        panel.add(headLabel2);
        panel.add(headLabel3);
        panel.add(headLabel4);


        int i = index;      //添加做题记录
        for( ;i < index + 2; i++) {
            if(i < exerciseFiles.length) {   //如果还有文件，则填充文件
                String[] record = recordOfExercises(exerciseFiles[i]);
                JLabel record1 = new JLabel(record[0]);
                JLabel record2 = new JLabel(record[1]);
                JLabel record3 = new JLabel(record[2]);
                panel.add(record1);
                panel.add(record2);
                panel.add(record3);
                JButton button = new JButton("温故而知新");
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {   //点击按钮继续做题
                        ContinueExercisesPanel panel1 = new ContinueExercisesPanel();
                        removeAll();
                        add(panel1);
                        validate();
                    }
                });
                panel.add(button);
            }else{  //如果文件读取完毕，则填充空表格，保证顺序
                System.out.println("哈哈");
                panel.add(new JLabel());
                panel.add(new JLabel());
                panel.add(new JLabel());
                panel.add(new JLabel());
            }
        }
        index = i;
        return panel;
    }

    private String[] recordOfExercises(String filePath){   //根据习题路径，找到习题，计算小明的做题数量和正确率，以字符串数组形式返回记录
        String[] record = new String[3];
        try {
            String exercisesPath = "src\\main\\resources\\"+filePath;
            exercises = FileOperator.readExercises(exercisesPath);
            answers = FileOperator.readAnswers(exercisesPath.replaceAll("exercises","answers"));
            results = FileOperator.readResults(exercisesPath.replaceAll("exercises","results"));
            record[0] = filePath.substring(0,4);
            record[1] = exercises.getSize()+":"+answers.size();
            int corretNums = 0;
            for(boolean b : results){
                if(b == true)
                    corretNums++;
            }
            record[2] = (corretNums/exercises.getSize()*100)+"";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return record;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(852,600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(new DisplayRecordPanel());
        frame.setVisible(true);
    }
}
